﻿using Angle.Krill.SystemConfig;
using Angle.Krill.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Angle.Krill.WinFile
{
    public class OperateFileHelper
    {
        private readonly static object obj = new object();
        private volatile static OperateFileHelper instance;

        private OperateFileHelper() { }
        /// <summary>
        /// 缓存
        /// </summary>
        public static OperateFileHelper Instance
        {
            get
            {
                if (null == instance)
                {
                    lock (obj)
                    {
                        instance = new OperateFileHelper();
                    }
                }

                return instance;
            }
        }

        public void NewFile(string physicalPath, Action<Form> BeforeShowDialog = null, Action<DialogResult> AfterShowDialog = null)
        {
            NewFile newFile = new NewFile(Path.GetDirectoryName(physicalPath));
            BeforeShowDialog?.Invoke(newFile);

            DialogResult dialogResult = newFile.ShowDialog();
            AfterShowDialog?.Invoke(dialogResult);
        }

        public void NewFolder(string physicalPath, Action<Form> BeforeShowDialog = null, Action<DialogResult> AfterShowDialog = null)
        {
            NewFolder newFolder = new NewFolder(physicalPath);
            BeforeShowDialog?.Invoke(newFolder);

            DialogResult dialogResult = newFolder.ShowDialog();
            AfterShowDialog?.Invoke(dialogResult);
        }
        public void DelFile(List<FileList> lst, Form form, Action okCallback, Action fileLockCallback)
        {
            if (lst.Count > 0)
            {
                AntdUI.Modal.open(new AntdUI.Modal.Config(form, "提示", $"确认删除文件？", AntdUI.TType.Warn)
                {
                    OnOk = config =>
                    {
                        foreach (var f in lst)
                        {
                            FileHelper.DelFile(f.PhysicalPath, fileLockCallback);
                        }
                        form.Success("文件删除成功");

                        okCallback?.Invoke();

                        return true;
                    }
                });
            }
            else
            {
                form.Info("请先选中需要删除的文件");
            }
        }
        public void DelFolderFile(string folderPath, Form form, Action okCallback)
        {
            try
            {
                if (!Directory.Exists(folderPath))
                {
                    form.Info("文件夹不存在！");
                }
                var lstFiles = FileHelper.GetFolderAndFiles(folderPath);
                if (lstFiles.FileCollection.Count > 0 || lstFiles.FolderCollection.Count > 0)
                {
                    AntdUI.Modal.open(new AntdUI.Modal.Config(form, "提示", $"文件夹中存在其它文件或文件夹\t\n确认删除？", AntdUI.TType.Warn)
                    {
                        OnOk = config =>
                        {
                            Directory.Delete(folderPath, true);
                            form.Success("文件夹删除成功");

                            okCallback?.Invoke();

                            return true;
                        }
                    });
                }
                else
                {
                    Directory.Delete(folderPath);
                    form.Success("文件夹删除成功");

                    okCallback?.Invoke();
                }
            }
            catch (IOException ex)
            {
                LoggerHelper.Error($"删除文件夹", ex);
                form.Error("文件夹无法进行操作，删除失败");
            }
            catch (UnauthorizedAccessException ex)
            {
                LoggerHelper.Error($"删除文件夹", ex);
                form.Error("文件夹无权访问，删除失败");
            }
            catch (Exception ex)
            {
                LoggerHelper.Error($"删除文件夹", ex);
                form.Error("删除失败");
            }
        }
    }
}
